package com.tx.productplus.server.service.impl;

import com.tx.productplus.common.DecreaseStockInput;
import com.tx.productplus.common.ProductInfoOutput;
import com.tx.productplus.server.dataobject.ProductInfo;
import com.tx.productplus.server.enums.ProductStatusEnum;
import com.tx.productplus.server.enums.ResultEnum;
import com.tx.productplus.server.exception.ProductException;
import com.tx.productplus.server.repository.ProductInfoRrpository;
import com.tx.productplus.server.service.ProductService;
import com.tx.productplus.server.utils.JsonUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoRrpository productInfoRrpository;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRrpository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public List<ProductInfoOutput> findList(List<String> productIdList) {
        return productInfoRrpository.findByProductIdIn(productIdList).stream()
                .map(e -> {
                    ProductInfoOutput output = new ProductInfoOutput();
                    BeanUtils.copyProperties(e, output);
                    return output;
                })
                .collect(Collectors.toList());
    }

    @Override
    public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) {
        List<ProductInfo> productInfoList = decreaseStockProcess(decreaseStockInputList);
        // 发送mq消息
        List<ProductInfoOutput> productInfoOutputList = productInfoList.stream().map(e -> {
            ProductInfoOutput productInfoOutput = new ProductInfoOutput();
            BeanUtils.copyProperties(e,productInfoOutput);
            return productInfoOutput;
        }).collect(Collectors.toList());
            amqpTemplate.convertAndSend("productInfo", JsonUtil.toJson(productInfoOutputList));
    }

    @Transactional
    public List<ProductInfo> decreaseStockProcess(List<DecreaseStockInput> cartDTOList) {
        List<ProductInfo> productInfoList = new ArrayList<>();

        for (DecreaseStockInput cartDTO:cartDTOList) {
            Optional<ProductInfo> productInfoOptional = productInfoRrpository.findById(cartDTO.getProductId());
            if (!productInfoOptional.isPresent()) {
                // 商品不存在
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            ProductInfo productInfo = productInfoOptional.get();
            Integer result = productInfo.getProductStock()-cartDTO.getProductQuantity();
            if (result<0) {
                // 库存不足
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);
            productInfoRrpository.save(productInfo);

            productInfoList.add(productInfo);
        }

        return productInfoList;
    }

}
